import axios from "axios";
import {Notification} from "element-ui";

import router from "@/router";


// Set config defaults when creating the instance
const instance = axios.create({
    baseURL: 'http://localhost:8999/',
    timeout: 5000
});


// 添加请求拦截器
instance.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么 basic  missgte  Bearer
    config.headers.Authorization = "Bearer " + localStorage.getItem("token");
    console.log(config)
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
instance.interceptors.response.use(function (response) {
    let {status, data, msg} = response.data;
    if (status == 20000) {
        return data;
    }
    if (status == 22233) {
        router.push("/login")
    } else {
        Notification.error({title: '错误信息', message: msg})
        return Promise.reject(false);
    }
}, function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
});


export default instance;
